// test/pages/check_ticket/check_ticket.js
const request = require("../../../utils/api.js")
let list = []
let  handleSearch
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:"",//票的id
        index:"",
        com_id: "",
        search: "",
        page: 1,
        dataList: [],
        flag: false,
        list: [],
        showModal:false,
        searchText: "",
        currentCategory: "1"
    },
    handleScrollTolower(e) {
        // console.log(1111);
        // this.setData({
        //     list
        // })
        this.setData({
            page: this.data.page + 1
        })
        this.getDataListPullDown(Number(this.data.currentCategory) - 1)

    },
    //获取上滑数据
    async getDataListPullDown(is_use) {
        const res = await request.fetchApi("Ticket/check_list", {
            com_id: this.data.com_id,
            is_use,
            search: this.data.search,
            page: this.data.page
        })

        if (res.list.length == 0) {

            wx.showToast({
                title: "无更多数据",
                icon: 'none',
                image: '',
                duration: 1500,
                mask: false,
            });
            setTimeout(() => {
                this.setData({
                    page: this.data.page - 1
                })
            }, 200);
        } else {
            res.list.forEach(item => {
                list.push(item)
            })
            this.setData({
                list
            })
        }


    },
    inputText(e) {
        this.setData({
            searchText: Number(e.detail.value)
        })
        handleSearch([Number(this.data.currentCategory) - 1,])
    },
    // ([Number(this.data.currentCategory) - 1,])
    // 防抖函数
    debounce(func){
        let timer = null
        return (args)=>{
            if(timer){
                clearTimeout(timer)
            }
            timer = setTimeout(()=>{
                func.apply(this,args)
            },1000)
        }
    },
    // 选择两个分类
    selectCategory(e) {
        list = []
        this.setData({
            currentCategory: e.currentTarget.id,
            page: 1
        })
        this.getDataList(Number(e.currentTarget.id) - 1)
    },
    // 立即检票
    gotoCheckTicket(e){
        const id = e.currentTarget.dataset.id
        const index = e.currentTarget.dataset.index
        this.setData({
            showModal:true,
            id,
            index
        })
    },
    cancel(){
        this.setData({
            showModal:false
        })
    },
    confirmTrue(){
        this.setData({
            showModal:false
        })
        wx.request({
            url: `https://edu.laigl.com/api/Ticket/check_t?id=${this.data.id}&com_id=${this.data.com_id}`,
            method: 'GET',
            success: (res) => {
                if(res.data.code == 2000){
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1500,
                        mask: false,
                    });
                    list.splice(this.data.index,1)
                    this.setData({
                        list
                    })
                }else{
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1500,
                        mask: false,
                    });
                }
            },
            fail: () => {},
        });
    },
    // 扫码
    scanCode() {
        console.log(this.data.com_id);
        wx.scanCode({
            onlyFromCamera: true,
            scanType: ['qrCode', 'barCode', 'datamatrix', 'pdf417'],
            success: (res) => {
                 wx.request({
                    url: `${res.result}&com_id=${this.data.com_id}`,
                    method: 'GET',
                    success: (res) => {
                        if(res.data.code == 2000){
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                                duration: 1500,
                                mask: true,
                            });
                        }
                        else{
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                                duration: 1500,
                                mask: true,
                            });
                        }
                    },
                    fail: () => {},
                }); 
            },
        });
    },
    //获取初始数据
    async getDataList(is_use) {
        const res = await request.fetchApi("Ticket/check_list", {
            com_id: this.data.com_id,
            is_use,
            search: this.data.searchText,
            page: this.data.page
        })
        wx.stopPullDownRefresh()
        list = res.list
        this.setData({
            dataList: res,
            list: res.list
        })
    },
    // 下拉刷新
    // refresh(e) {
    //     this.setData({
    //         page: 1
    //     })
    //     this.getDataList(Number(this.data.currentCategory) - 1)
    // },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            com_id: options.com_id
        })
        this.getDataList(0)
        handleSearch =  this.debounce(this.getDataList)

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            page: 1
        })
        this.getDataList(Number(this.data.currentCategory) - 1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        
        this.setData({
            page: this.data.page + 1
        })
        this.getDataListPullDown(Number(this.data.currentCategory) - 1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})